package com.example.myapplication14_3;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.MediaController;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private VideoView videoView;
    private Button btnPlay, btnStop;
    private MediaController mediaController;
    private String currentVideoPath;
    private MediaPlayer mediaPlayer;
    private boolean isMuted = false; // 新增：记录静音状态

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化视频播放器
        videoView = findViewById(R.id.videoView);

        // 初始化播放和停止按钮
        btnPlay = findViewById(R.id.btnPlay);
        btnStop = findViewById(R.id.btnStop);

        // 初始化静音开关
        Switch swMute = findViewById(R.id.swMute);
        swMute.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isMuted = isChecked; // 更新静音状态
                if (mediaPlayer != null) {
                    mediaPlayer.setVolume(isMuted ? 0 : 1, isMuted ? 0 : 1);
                }
            }
        });

        // 初始化视频选择器
        String[] videoFiles = {"testvideo", "video1", "video2"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, videoFiles);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner videoSpinner = findViewById(R.id.videoSpinner);
        videoSpinner.setAdapter(adapter);

        // 设置默认视频路径
        currentVideoPath = "android.resource://" + getPackageName() + "/raw/testvideo";
        setupVideo(currentVideoPath);

        // Spinner 选择监听器
        videoSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String videoName = videoFiles[position];
                String videoPath = "android.resource://" + getPackageName() + "/raw/" + videoName;
                currentVideoPath = videoPath;
                setupVideo(videoPath); // 重新设置视频源
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Do nothing
            }
        });

        // 播放按钮点击事件
        btnPlay.setOnClickListener(v -> {
            if (videoView.isPlaying()) {
                videoView.pause();
                btnPlay.setText("播放");
            } else {
                videoView.start();
                btnPlay.setText("暂停");
            }
        });

        // 停止按钮点击事件
        btnStop.setOnClickListener(v -> {
            videoView.stopPlayback();
            setupVideo(currentVideoPath); // 重新设置视频源
            btnPlay.setText("播放");
        });
    }

    // 配置视频的方法
    private void setupVideo(String videoPath) {
        videoView.setVideoURI(Uri.parse(videoPath));
        videoView.setMediaController(null); // 重置控制器
        mediaController = new MediaController(this);
        videoView.setMediaController(mediaController);
        mediaController.setAnchorView(videoView);

        videoView.setOnPreparedListener(mp -> {
            mediaPlayer = mp;
            mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);
            mp.setLooping(false); // 关闭循环播放
            // 应用当前静音状态到新的 MediaPlayer 实例
            mp.setVolume(isMuted ? 0 : 1, isMuted ? 0 : 1);
            if (!videoView.isPlaying()) {
                videoView.pause(); // 默认暂停
                btnPlay.setText("播放");
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (videoView.isPlaying()) {
            videoView.pause();
            btnPlay.setText("播放");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (videoView != null) {
            videoView.stopPlayback();
        }
    }
}